package com.zhkj.txg.module.home.adapter

import android.view.View
import android.widget.ImageView
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.gson.Gson
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SPUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.CategorySecondEntity
import com.zhkj.txg.module.home.entity.CategoryThirdEntity
import com.zhkj.txg.module.home.entity.ShopInfo
import com.zhkj.txg.module.home.ui.ProductListActivity
import com.zhkj.txg.utils.ImageUtils
import java.lang.Exception

class CategoryAllRightAdapter : BaseNodeAdapter() {

    companion object {
        val TYPE_SECOND = 0
        val TYPE_THIRD = 1
    }

    init {
        addFullSpanNodeProvider(SecondCateProvider())
        addNodeProvider(ThirdCateProvider())
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        return when (data[position]) {
            is CategoryThirdEntity -> TYPE_THIRD
            else -> TYPE_SECOND
        }
    }
}

class SecondCateProvider : BaseNodeProvider() {
    override val itemViewType: Int = CategoryAllRightAdapter.TYPE_SECOND
    override val layoutId: Int = R.layout.item_cate_second

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? CategorySecondEntity)?.let {
            helper.setText(R.id.tvCateSecond, data.name)
        }
    }
}

class ThirdCateProvider : BaseNodeProvider() {
    override val itemViewType: Int = CategoryAllRightAdapter.TYPE_THIRD
    override val layoutId: Int = R.layout.item_cate_third

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? CategoryThirdEntity)?.let {
            val imageView = helper.setText(R.id.tvCate, data.name).getView<ImageView>(R.id.ivCate)
            ImageUtils.loadRoundCornerImage(context, imageView, data.image)
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        (data as? CategoryThirdEntity)?.let {
            ProductListActivity.categoryOpen(context, data.name, data.id)
        }
    }
}